package com.base.engine.input;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.base.engine.MADEngine;
import com.base.engine.error.EMADErrorCode;
import com.base.engine.error.MADErrorHandler;
import com.base.engine.game.MADGame;
import com.base.engine.graphics.MADBasicGL;
import com.base.engine.gui.MADButton;
import com.base.engine.gui.MADUserInterface;

import static org.lwjgl.util.glu.GLU.* ; 
import static org.lwjgl.opengl.GL11.* ;

public final class MADInputHandler {

	
	public static void getKeyboardInput(MADGame g){
		if(!(g == null)){
			while(Keyboard.next()){
				if(Keyboard.getEventKeyState()){
					switch(Keyboard.getEventKey()){
					case Keyboard.KEY_F2: MADUserInterface.getDebuggingScreen().clearScreen(); break ; 
					case Keyboard.KEY_F3: MADUserInterface.getDebuggingScreen().setVisible(!MADUserInterface.getDebuggingScreen().isVisible()); 
						break ; 
					case Keyboard.KEY_F11: MADEngine.setFullscreen(!MADEngine.isFullscreen()); break ; 
					case Keyboard.KEY_ESCAPE: 
						if(MADUserInterface.getMenuScreen().isVisible()){
							MADUserInterface.getMenuScreen().deactivate(); 
						}
						else {
							MADUserInterface.getMenuScreen().activate(); 
						}
					}
				}
			}	
		}else
			MADErrorHandler.throwException(EMADErrorCode.GAME_REF, new NullPointerException("Invalid game - Game reference is NULL!")); 
	}
	
	public static void getMouseInput(MADGame g){

		if(MADUserInterface.getMenuScreen().isVisible()){
			float mx = Mouse.getX(); 
			float my = Mouse.getY(); 
	
			for(MADButton b: MADUserInterface.getMenuScreen().getButtons()){
				b.deactivate(); 
				if(b.isInSensitiveeArea(mx, MADEngine.HEIGHT - my)){
					b.activate();
				}
			}
		}
		
		if(!(g == null)){
			while(Mouse.next()){
				Mouse.setGrabbed(false);
				if(Mouse.getEventButtonState()){
					switch(Mouse.getEventButton()){
					case 0: 
						if (MADUserInterface.getMenuScreen().isVisible())
							for (MADButton b : MADUserInterface.getMenuScreen()
									.getButtons()) {
								if (b.isActive())
									b.onClick();
							}
					}
				}
			}
			
		}else
			MADErrorHandler.throwException(EMADErrorCode.GAME_REF, new NullPointerException("Invalid game - Game reference is NULL!"));  
	}
	
}
